package com.china08.yxyapi.api.manage.sch.platform.live.lesson;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.live.YxyLiveLesson;
import com.china08.yxyapi.entity.db.live.YxyLiveLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonItemRepository;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class LiveSchLessonCrtController extends YxyApiBaseController {

    @Autowired
    private YxyLiveLessonRepository yxyLiveLessonRepository;

    @Autowired
    private YxyLiveLessonItemRepository yxyLiveLessonItemRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/platform/live/lesson/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) throws ParseException {

        String schoolId = getProfile(request, response).getSchoolId();
        String userId = getProfile(request, response).getId();

        YxyLiveLesson lesson = new YxyLiveLesson();
        Integer orderNum = yxyLiveLessonRepository.countByCreaterUserSchoolId(schoolId) + 1;
        lesson.setAuditStatus(0);
        lesson.setName(reqModel.lessonName);
        lesson.setSummary(reqModel.summary);
        lesson.setCover(reqModel.cover);
        lesson.setAuditStatus(0);
        lesson.setAvator("");
        lesson.setCatalogId1(reqModel.catalog1);
        lesson.setCatalogId2(reqModel.catalog2);
        lesson.setCatalogId3(reqModel.catalog3);
        lesson.setCatalogSchoolId(schoolId);
        lesson.setCount(reqModel.items.size());
        lesson.setCreateDate(new Date());
        lesson.setCreaterUserId(userId);
        lesson.setCreaterUserSchoolId(schoolId);
        lesson.setDownReason("");
        lesson.setLiveType(reqModel.liveType);
        lesson.setOrderNum(orderNum);
        lesson.setPv(0);
        lesson.setStatus(0);
        lesson.setTeacherId(reqModel.teacherId);
        lesson.setTeacherMobile(reqModel.teacherMobile);
        lesson.setTeacherNick(reqModel.teacherNick);
        lesson.setTeacherSchoolId(reqModel.teacherSchoolId);
        lesson.setTeacherSchoolNick(reqModel.teacherSchoolNick);
        lesson.setUpDate(new Date());
        lesson.setIsVisible(0);
        lesson.setVv(0);
        lesson.setScope(0);
        yxyLiveLessonRepository.save(lesson);

        String lessonId = lesson.getId();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<YxyLiveLessonItem> itemlList = new ArrayList<YxyLiveLessonItem>();
        // 保存课时
        List<Pojo> items = reqModel.items;
        Integer num = 1;
        for (Pojo pojo : items) {
            YxyLiveLessonItem item = new YxyLiveLessonItem();
            item.setLessonId(lessonId);
            item.setStatus(0);
            String beginDate = pojo.bTime + ":00";
            item.setBeginDate(sdf.parse(beginDate));
            item.setEndDate(sdf.parse(pojo.eTime + ":00"));
            item.setItemNo(num);
            itemlList.add(item);
            num++;
        }
        yxyLiveLessonItemRepository.save(itemlList);

        RespModel model = new RespModel();
        model.setMessage("添加成功");
        return model;
    }

    public static class Pojo {
        public String bTime;
        public String eTime;
        public String hour;
        public String itemDate;
        public String min;
        public Integer num;
        public String times;
    }

    public static class ReqModel extends YxyApiReqModel {
        // 课程名称
        public String lessonName;
        // 课程简介
        public String summary;
        // 课程封面
        public String cover;
        // 主讲人id
        public String teacherId;
        // 教师手机号
        public String teacherMobile;
        // 教师名称
        public String teacherNick;
        // 主讲人学校id
        public String teacherSchoolId;
        // 主讲人学校名称
        public String teacherSchoolNick;
        // 直播方式
        public Integer liveType;
        // 一级目录
        public String catalog1;
        // 二级目录
        public String catalog2;
        // 三级目录
        public String catalog3;
        // 课时
        public List<Pojo> items;
    }

    public static class RespModel extends YxyApiRespModel {
    }

}
